import Fluent
import FluentMySQLDriver

// 创建一个数据模型
final class User: Model, Content {
    static let schema = "users"
    
    @ID(key: .id)
    var id: UUID?
    
    @Field(key: "name")
    var name: String
    
    @Field(key: "email")
    var email: String
    
    init() {}
    
    init(id: UUID? = nil, name: String, email: String) {
        self.id = id
        self.name = name
        self.email = email
    }
}

// 连接到MySQL数据库
let database = MySQLDatabase(
    config: MySQLDatabaseConfig(
        hostname: "localhost",
        port: 3306,
        username: "root",
        password: "password",
        database: "mydatabase"
    )
)
let migrationConfig = MigrationConfig()
migrationConfig.add(model: User.self, database: .mysql)
let app = Application(.testing)
defer { app.shutdown() }
app.databases.use(database, as: .mysql)
app.migrations.add(migrationConfig)
try app.autoMigrate().wait()

// 插入数据
let user = User(name: "John Doe", email: "john.doe@example.com")
_ = user.create(on: app.db)

// 查询数据
let users = User.query(on: app.db).all()
print(users)